package handlers

import (
	"github.com/gin-gonic/gin"
	"stroe-ease/models"
)

func ShopAdd(c *gin.Context) {
	name := c.PostForm("name")
	desc := c.PostForm("desc")
	address := c.PostForm("address")
	if name == "" || desc == "" || address == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Shop{
		Name:    name,
		Desc:    desc,
		Address: address,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func ShopList(c *gin.Context) {
	var shop []models.Shop
	models.DB.Find(&shop)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": shop,
	})
	return
}
func UpShop(c *gin.Context) {
	id := c.Query("id")
	name := c.Query("name")
	var shop models.Shop
	err := models.DB.Model(shop).Where("id = ?", id).Update("name", name).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "修改失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
	})
	return
}
